package com.pss.common.component;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取登录用户信息
 */
@Component
public class LoginUserHolder {
    public Map getCurrentUser(){
        //从Header中获取用户信息
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String userStr = request.getHeader("user");
        Map map = JSON.parseObject(userStr, Map.class);
        Map data = new HashMap<>();
        data.put("user_name", map.get("user_name"));
        data.put("id", map.get("id"));
        data.put("authorities", map.get("authorities"));
        return data;
    }
}
